﻿using System;
using System.Collections.Generic;
using TeamsPortal.DataAccess;

namespace TeamsPortal.ServiceClient
{
    public class ForumsServiceClient : IForumsServiceClient
	{
		/// <summary>
		/// Gets a list of ForumCategories with the list forums, dependant of the user role.
		/// </summary>
		public List<ForumCategory> GetList(int userId)
		{
			var da = new ForumsDataAccess();
            return da.GetList(userId);
		}

		/// <summary>
		/// Get all categories (without the forums in it).
		/// </summary>
		public List<ForumCategory> GetCategories()
		{
			var da = new CategoriesDataAccess();
			return da.GetCategories();
		}

		public Forum Get(string shortName)
		{
			var da = new ForumsDataAccess();
			return da.GetByShortName(shortName);
		}

		public void Add(Forum forum, int userId)
		{
			forum.ValidateFields();
			SetAvailableShortName(forum);
			var da = new ForumsDataAccess();
			da.Add(forum, userId);
		}

		/// <summary>
		/// Validates if a forum shortname is already taken
		/// </summary>
		/// <exception cref="ArgumentNullException"></exception>
		public void SetAvailableShortName(Forum forum)
		{
			if (forum == null || forum.ShortName == null)
			{
				throw new ArgumentNullException("Forum.ShortName");
			}
			var da = new ForumsDataAccess();
			forum.ShortName = da.GetAvailableShortName(forum.ShortName);
		}

		public void Edit(Forum forum, int userId)
		{
			forum.ValidateFields();
			var da = new ForumsDataAccess();
			da.Edit(forum, userId);
		}

		/// <summary>
		/// Deletes a forum from the system (not a phisical delete).
		/// </summary>
		/// <param name="forum"></param>
		/// <returns></returns>
		public bool Delete(string forum)
		{
			var da = new ForumsDataAccess();
			return da.Delete(forum);
		}
	}
}
